(function () {
    var SaveObj = {};
    $(function () {
        loadData().then(function () {
				$(".mainShopList").show()
            var pid = window.location.search.split("?")[1].split("=")[1];
            $.ajax({
                url: "./../server/getGoods.php",
                data: {
                    pid
                },
                dataType: "json"
            }).then(function (res) {
                SaveObj = res;
                $(".titlename").text(res.title);
                $(".goodsname").text(res.name);
                var price=res.price.slice(0,1)+","+res.price.slice(1)
                $(".goodprice").text(price);
                $(".goodscolor").text(res.colors.color);
                SaveObj["colors"] = res.colors.color
                $(".smallul>li>img").attr("src", "./../server/" + res.imgUrl);
                $(".bigArea>img").attr("src", "./../server/" + res.imgUrl);
                $(".colorul>li>a>img").attr("src", "./../server/" + res.imgUrl);
                var $li1 = ``;
                res.smallImgUrl.forEach(function (el, index) {
                    $li1 +=
                        `<li class="leftImgli"><img src='./../server/${el.src}'></li>`
                })
                $(".imgBtm>ul").html($li1)

                var $li2 = ``;
                res.size.forEach(function (el, index) {
                    $li2 +=
                        `<li><a class="selectSize">${el.size}</a></li>`
                })
                $(".cmselect").html($li2);
            })
        }, function () {
            $(".mainShopList").hide()
            layer.confirm("你还没登录", {
                    icon: 3,
                    btn: ["立即登录", "暂不登录"]
                }, function () {
                    window.location.href = "./loading.html";
                },
                function () {
                    return
                })
        })


        //页面加载后,判断是否登录,如果没登录,请先登录
        function loadData() {
            var defer = $.Deferred(); //等价于定义了一个 promise对象
            $.ajax({
                url: "./../server/isLogin.php",
                dataType: "json"
            }).then(function (res) {

                if (res.status == true) {
                    defer.resolve();
                } else {
                    defer.reject();
                }
            })
            return defer;
        }
    })

    $(function () {
        let size2 = "";
        //获取尺寸
        $(".box").on("click", ".selectSize",function() {

            if($(this).hasClass("selcetedsize")){
                $(this).removeClass("selcetedsize")
                size2 = ""
            }else{
                $(this).addClass("selcetedsize").parent("li").siblings("li").find("a").removeClass(
                    "selcetedsize")
                size2=$(this).text();
                SaveObj.size2=size2;
            }
        })

        //获取数量
        $(".box").on("click",".popnum",function(){
            $(".shopnum").val($(".shopnum").val()-1)
            if($(".shopnum").val()<=1){
                $(".shopnum").val("1")
            }
        })
        $(".box").on("click",".addnum",function(){
            $(".shopnum").val(Number($(".shopnum").val())+1)

        })

        //加入购物车
        $(".box").on("click",".addcarts",function() {

            if(size2==""){
                layer.msg("请选择衣服尺寸");
                return;
            }

            SaveObj["uid"] = JSON.parse(localStorage.getItem("user")).id;
            SaveObj["number"] = $(".shopnum").val();
            SaveObj["total"] = Number($(".shopnum").val())*Number(SaveObj["price"].replace(",",""));
            console.log(SaveObj)
            $.ajax({
                url:"./../server/addCarts.php",
                data:SaveObj,
                dataType:"json",
                type:"post"
            }).then(function (res) {
                var totalprice=Number($(".addshopcar").text().replace("¥",""))

                if(res.status==1||res.status==3){
                    layer.msg("加入购物车成功")
                    $(".addshopcar").text("¥"+(totalprice+Number($(".shopnum").val())*Number(SaveObj["price"].replace(",","")))+".00")
                }
            if(res.status==1){
                var length=$(".hidegoodslist").length
                var $li =
                    `<li class="hidegoodslist" data-tmpindex="${length}">
                    					<div class='smallshopimg'>
                    					<a href='#'><img src='${SaveObj.imgUrl}'></a>
                    					</div>
                    					<div class='shoptext'>
                    						<a href='#'>${SaveObj.name}</a>
                    						<a href='#' class="goodssize">尺寸:${SaveObj.size2}</a>
                    						<a href='javascript:;;' class='closebtn' data-total="${SaveObj.total}">删除</a>
                    					</div>
                    				</li>`
                $(".goodsListUL").append($li)
                $(".listnum").text(length+1)
                $(".addshopcar").text("¥"+(totalprice+Number($(".shopnum").val())*Number(SaveObj["price"].replace(",","")))+".00")
                $(".shopping").slideDown(500, function() {
                    setTimeout(() => {
                        $(".shopping").slideUp(500)
                    }, 5000)
                })
            }
            })

            //更新购物包里的数量和价格
            // $.ajax({
            //     url:"./../server/getgoodslist.php",
            //     dataType:"json",
            //     type:"post"
            // }).then(function (res) {
                
            //     if(res.status==1){
            //         console.log(res.data.length)
                    
            //         res.data.forEach(function (el,index) {
            //             totalprice+=Number(el.total)
            //         })
            //        
            //        
            //     }
            // })



        })
        //立即购买跳转结算页面
        $(".box").on("click",".rightBuy",function () {
            if(size2==""){
                layer.msg("请选择衣服尺寸");
                return;
            }
            location.href="./shoppingcar.html"
        })

    })



    $(function () {
    //倒计时
        function  cutTime(time) {
            var d2=Date.parse(time);
            var timer=setInterval(function () {
                var d1=new Date()
                var num=d2-d1;
                var dd=parseInt(num/1000/60/60/24);
                dd = dd < 10 ? "0" + dd : dd;
                var hh=parseInt(num/1000/60/60)%24;
                hh = hh < 10 ? "0" + hh : hh;
                var mm=parseInt(num/1000/60)%60;
                mm = mm < 10 ? "0" + mm : mm;
                var ss=parseInt(num/1000)%60;
                ss = ss < 10 ? "0" + ss : ss;
                $(".day").text(dd)
                $(".hours").text(hh)
                $(".minute").text(mm)
                $(".second").text(ss)
                if(num<=0){
                    clearInterval(timer)
                }
            },1000)
        }

        cutTime("2019-7-11 16:41:50")

        //放大镜换图
        $(".box").on("click",".leftImgli",function () {
            var imgstr=$(this).find("img").attr("src").replace("_1","")
            $(".lefttopImg").removeAttr("src").prop("src",imgstr);
            $(".bigImg").removeAttr("src").prop("src",imgstr)
        })


    })



})()